package com.zhongxi.guoyuan.adapter.rewardCoin;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.DataUtil;

import java.util.List;

import static com.zhongxi.guoyuan.R.id.tv_reward_coin;

/**
 * Created by Administrator on 2016/10/29.
 */

public class RewardCoinAdapter extends BaseAdapter {
    private List<HotGoodsData> data;
    private LayoutInflater mLayoutInflater;
    private Context mContext;

    public RewardCoinAdapter(Context context, List<HotGoodsData> data) {
        this.mLayoutInflater = LayoutInflater.from(context);
        this.data = data;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        if (data != null && data.size() > 0) {
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.item_reward_coin, parent, false);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tv_reward_price = (TextView) convertView.findViewById(R.id.tv_reward_price);
            holder.tv_reward_yield = (TextView) convertView.findViewById(R.id.tv_reward_yield);
            holder.tv_reward_coin = (TextView) convertView.findViewById(tv_reward_coin);
            holder.tvView = (TextView) convertView.findViewById(R.id.tvView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (null != data.get(position) && !"".equals(data.get(position))) {
            HotGoodsData list = data.get(position);
            if (null != list.getThumbnail() && !"".equals(list.getThumbnail())) {
                Glide.with(mContext).load(Url.IMG_URL + list.getThumbnail()).placeholder(R.mipmap.iv_loading).fitCenter().into(holder.ivImg);
            }
            if (null != list.getName() && !"".equals(list.getName())) {
                holder.tvName.setText(list.getName());
            } else {
                holder.tvName.setText("");
            }
            if (TextUtils.isEmpty(list.getFloorprice())) {
                holder.tv_reward_price.setText("0 元");
            } else {
                holder.tv_reward_price.setText(list.getFloorprice() + " 元");
            }
            switch (list.getGoodstype()) {
                case "1":
                    if (TextUtils.isEmpty(list.getFloorprice())) {
                        holder.tv_reward_coin.setText("");
                        holder.tv_reward_yield.setText("");
                    } else {
                        holder.tv_reward_coin.setText(mContext.getResources().getString(R.string.give_meidou) + CommonTool.getWhole(list.getFloorprice()) + " 美豆");
                        holder.tv_reward_coin.setTextColor(mContext.getResources().getColor(R.color.postCartItem));
                        holder.tv_reward_yield.setText(mContext.getResources().getString(R.string.more_give) + DataUtil.multiplys(list.getFloorprice(), "0.05",2) + "红豆");
                        holder.tv_reward_yield.setTextColor(mContext.getResources().getColor(R.color.title_red));
                    }
                    break;
                case "3":
                    if (TextUtils.isEmpty(list.getFloorprice())) {
                        holder.tv_reward_coin.setText("");
                    } else {
                        holder.tv_reward_coin.setText(mContext.getResources().getString(R.string.award_hongdou) + list.getFloorprice() + " 红豆");
                        holder.tv_reward_coin.setTextColor(mContext.getResources().getColor(R.color.title_red));
                        holder.tv_reward_yield.setVisibility(View.GONE);
                    }
                    break;
            }
            if (null != list.getOrdernum() && !"".equals(list.getOrdernum())) {
                holder.tvView.setText("销量：" + list.getOrdernum());
            } else {
                holder.tvView.setText("0");
            }
        }
        return convertView;
    }

    class ViewHolder {
        private ImageView ivImg;
        private TextView tvName;
        //        private TextView tvDesc;
        private TextView tv_reward_price;
        private TextView tv_reward_yield;
        private TextView tv_reward_coin;
        private TextView tvView;
    }
}
